package com.ai.zhly.util.sms.utils;

import java.io.IOException;

import com.ai.zhly.util.sms.msg.MsgHead;
import com.ai.zhly.util.sms.msg.Msg_Cmpp_Active_Test;
import com.ai.zhly.util.sms.msg.Msg_Cmpp_Active_Test_Resp;
import com.ai.zhly.util.sms.msg.Msg_Cmpp_Connect;
import com.ai.zhly.util.sms.msg.Msg_Cmpp_Connect_Resp;
import com.ai.zhly.util.sms.msg.Msg_Cmpp_Submit;
import com.ai.zhly.util.sms.msg.Msg_Cmpp_Submit_Resp;
import com.ai.zhly.util.sms.msg.Msg_Command;

/**
 * 
 * 
 * @company：AsiaInfo BDX-DS-PSO NFJD/XZ/GD Dept.-GZ
 * @author：xw.jiang on 2015年11月13日
 * @email:jiangxw@asiainfo.com
 * @description: 打包解包工具 A.对发往ISMG的消息进行打包操作 B.对ISMG发来的回应消息进行解包操作
 */
public class MsgUtils {
	/**
	 * 1:打包
	 * 
	 * @param msg
	 *            需要打包的消息类
	 * @return
	 */
	public static byte[] packMsg(MsgHead msg) {
		java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
		java.io.DataOutputStream dos = new java.io.DataOutputStream(bos);
		int command = msg.getCommand_Id();
		byte[] data = null;
		try {
			switch (command) {
			// 发送【登录链接】CMPP_CONNECT消息类型时 处理如下
			case Msg_Command.CMPP_CONNECT:
				Msg_Cmpp_Connect cmpp_connect = (Msg_Cmpp_Connect) msg;

				dos.writeInt(cmpp_connect.getTotal_Length());
				dos.writeInt(cmpp_connect.getCommand_Id());
				dos.writeInt(cmpp_connect.getSequence_Id());

				Utils.writeFully(dos, 6, cmpp_connect.getSource_Addr());
				dos.write(cmpp_connect.getAuthenticatorSource());
				dos.writeByte(cmpp_connect.getVersion());
				dos.writeInt(cmpp_connect.getTimestamp());
				dos.flush();
				data = bos.toByteArray();
				break;
			/// 发送【短信内容】CMPP_SUBMIT消息类型时 处理如下
			case Msg_Command.CMPP_SUBMIT:// 打包Submit
				Msg_Cmpp_Submit ms = (Msg_Cmpp_Submit) msg;

				dos.writeInt(ms.getTotal_Length());
				dos.writeInt(ms.getCommand_Id());
				dos.writeInt(ms.getSequence_Id());

				dos.writeLong(ms.getMsg_Id());// 8
				dos.writeByte(ms.getPk_total());// 1
				dos.writeByte(ms.getPk_number());// 1

				dos.writeByte(ms.getRegistered_Delivery());// 1
				dos.writeByte(ms.getMsg_level());// 1

				Utils.writeFully(dos, 10, ms.getService_Id());// 10

				dos.writeByte(ms.getFee_UserType());// 1
				Utils.writeFully(dos, 21, ms.getFee_terminal_Id());// 21
				dos.writeByte(ms.gettP_pId()); // 1
				dos.writeByte(ms.gettP_udhi());// 1
				dos.writeByte(ms.getMsg_Fmt());// 1

				Utils.writeFully(dos, 6, ms.getMsg_src());// 6
				Utils.writeFully(dos, 2, ms.getFeeType());// 2
				Utils.writeFully(dos, 6, ms.getFeeCode());// 6
				Utils.writeFully(dos, 17, ms.getValId_Time());// 17
				Utils.writeFully(dos, 17, ms.getAt_Time());// 17

				Utils.writeFully(dos, 21, ms.getSrc_Id());// 21
				dos.writeByte(ms.getDestUsr_tl());// 1

				Utils.writeFully(dos, 21, ms.getDest_terminal_Id()); // 21
				dos.writeByte(ms.getMsg_Length()); // 1
				Utils.writeFully(dos, ms.getMsg_Length(), ms.getMsg_Content());
				Utils.writeFully(dos, 8, ms.getReserve());
				dos.flush();
				data = bos.toByteArray();
				break;
			// 发送【链路检测】CMPP_ACTIVE_TEST消息类型时 处理如下
			case Msg_Command.CMPP_ACTIVE_TEST:
				Msg_Cmpp_Active_Test mst = (Msg_Cmpp_Active_Test) msg;
				dos.writeInt(mst.getTotal_Length());
				dos.writeInt(mst.getCommand_Id());
				dos.writeInt(mst.getSequence_Id());
				dos.flush();
				data = bos.toByteArray();
				break;
			}
			return data;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 1:解包
	 * 
	 * @param msg
	 *            需要解包的字节数据
	 * @return 消息包
	 */
	public static MsgHead praseMsg(byte[] msg) {
		java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(
				msg);
		java.io.DataInputStream dis = new java.io.DataInputStream(bais);
		try {
			int command_Id = dis.readInt();
			int sequenceId = dis.readInt();
			switch (command_Id) {
			// 解包登录connect应答消息
			case Msg_Command.CMPP_CONNECT_RESP:
				Msg_Cmpp_Connect_Resp msg_resp = new Msg_Cmpp_Connect_Resp();
				int status = dis.readByte();
				byte[] auth = Utils.readString(dis, 16).getBytes();
				byte verSion = dis.readByte();
				msg_resp.setTotal_Length(4 + 4 + 1 + 16 + 1); // command_id +
																// seq + status
																// + auth +
																// version
				msg_resp.setCommand_Id(command_Id);
				msg_resp.setSequence_Id(sequenceId);
				msg_resp.setStatus(status);
				msg_resp.setAuthenticatorISMG(auth);
				msg_resp.setVersion(verSion);
				return msg_resp;
			case Msg_Command.CMPP_SUBMIT_RESP:// 要解包Submit应答消息
				Msg_Cmpp_Submit_Resp msr = new Msg_Cmpp_Submit_Resp();
				msr.setTotal_Length(msg.length + 4);
				msr.setCommand_Id(command_Id);
				msr.setSequence_Id(sequenceId);
				msr.setMsg_Id(dis.readLong());
				msr.setResult(dis.readByte());
				return msr;
			case Msg_Command.CMPP_ACTIVE_TEST_RESP:// 解包链路检查应答消息
				Msg_Cmpp_Active_Test_Resp mtr = new Msg_Cmpp_Active_Test_Resp();
				mtr.setTotal_Length(msg.length + 4);
				mtr.setCommand_Id(command_Id);
				mtr.setSequence_Id(sequenceId);
				return mtr;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}
}
